// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.inventory;

import zombie.core.Core;
import zombie.core.Rand;
import zombie.debug.DebugLog;
import zombie.inventory.types.Drainable;
import zombie.inventory.types.Food;
import zombie.inventory.types.Moveable;
import zombie.inventory.types.Radio;
import zombie.network.GameClient;
import zombie.scripting.ScriptManager;
import zombie.scripting.objects.Item;
import zombie.util.Type;
import zombie.world.ItemInfo;
import zombie.world.WorldDictionary;

public final class InventoryItemFactory {
    public static InventoryItem CreateItem(String itemType) {
        return CreateItem(itemType, 1.0F);
    }

    public static InventoryItem CreateItem(String itemType, Food food) {
        InventoryItem inventoryItem = CreateItem(itemType, 1.0F);
        Food _food = Type.tryCastTo(inventoryItem, Food.class);
        if (_food == null) {
            return null;
        } else {
            _food.setBaseHunger(food.getBaseHunger());
            _food.setHungChange(food.getHungChange());
            _food.setBoredomChange(food.getBoredomChangeUnmodified());
            _food.setUnhappyChange(food.getUnhappyChangeUnmodified());
            _food.setCarbohydrates(food.getCarbohydrates());
            _food.setLipids(food.getLipids());
            _food.setProteins(food.getProteins());
            _food.setCalories(food.getCalories());
            return inventoryItem;
        }
    }

    public static InventoryItem CreateItem(String itemType, float useDelta) {
        return CreateItem(itemType, useDelta, true);
    }

    public static InventoryItem CreateItem(String itemType, float useDelta, boolean moduleDefaultsToBase) {
        InventoryItem inventoryItem = null;
        Item item = null;
        boolean _boolean = false;
        String string0 = null;

        try {
            if (itemType.startsWith("Moveables.") && !itemType.equalsIgnoreCase("Moveables.Moveable")) {
                String[] string1 = itemType.split("\\.");
                string0 = string1[1];
                _boolean = true;
                itemType = "Moveables.Moveable";
            }

            item = ScriptManager.instance.FindItem(itemType, moduleDefaultsToBase);
        } catch (Exception exception) {
            DebugLog.log("couldn't find item " + itemType);
        }

        if (item == null) {
            return null;
        } else {
            inventoryItem = item.InstanceItem(null);
            if (GameClient.bClient && (Core.getInstance().getPoisonousBerry() == null || Core.getInstance().getPoisonousBerry().isEmpty())) {
                Core.getInstance().setPoisonousBerry(GameClient.poisonousBerry);
            }

            if (GameClient.bClient && (Core.getInstance().getPoisonousMushroom() == null || Core.getInstance().getPoisonousMushroom().isEmpty())) {
                Core.getInstance().setPoisonousMushroom(GameClient.poisonousMushroom);
            }

            if (itemType.equals(Core.getInstance().getPoisonousBerry())) {
                ((Food)inventoryItem).Poison = true;
                ((Food)inventoryItem).setPoisonLevelForRecipe(1);
                ((Food)inventoryItem).setPoisonDetectionLevel(1);
                ((Food)inventoryItem).setPoisonPower(5);
                ((Food)inventoryItem).setUseForPoison(new Float(Math.abs(((Food)inventoryItem).getHungChange()) * 100.0F).intValue());
            }

            if (itemType.equals(Core.getInstance().getPoisonousMushroom())) {
                ((Food)inventoryItem).Poison = true;
                ((Food)inventoryItem).setPoisonLevelForRecipe(2);
                ((Food)inventoryItem).setPoisonDetectionLevel(2);
                ((Food)inventoryItem).setPoisonPower(10);
                ((Food)inventoryItem).setUseForPoison(new Float(Math.abs(((Food)inventoryItem).getHungChange()) * 100.0F).intValue());
            }

            inventoryItem.id = Rand.Next(2146250223) + 1233423;
            if (inventoryItem instanceof Drainable) {
                ((Drainable)inventoryItem).setUsedDelta(useDelta);
            }

            if (_boolean) {
                inventoryItem.type = string0;
                inventoryItem.fullType = inventoryItem.module + "." + string0;
                if (inventoryItem instanceof Moveable && !((Moveable)inventoryItem).ReadFromWorldSprite(string0) && inventoryItem instanceof Radio) {
                    DebugLog.log("InventoryItemFactory -> Radio item = " + (itemType != null ? itemType : "unknown"));
                }
            }

            return inventoryItem;
        }
    }

    public static InventoryItem CreateItem(String itemType, float useDelta, String param) {
        InventoryItem inventoryItem = null;
        Item item = ScriptManager.instance.getItem(itemType);
        if (item == null) {
            DebugLog.log(itemType + " item not found.");
            return null;
        } else {
            inventoryItem = item.InstanceItem(param);
            if (inventoryItem == null) {
            }

            if (inventoryItem instanceof Drainable) {
                ((Drainable)inventoryItem).setUsedDelta(useDelta);
            }

            return inventoryItem;
        }
    }

    public static InventoryItem CreateItem(String module, String name, String type, String tex) {
        InventoryItem inventoryItem = new InventoryItem(module, name, type, tex);
        inventoryItem.id = Rand.Next(2146250223) + 1233423;
        return inventoryItem;
    }

    public static InventoryItem CreateItem(short registryID) {
        ItemInfo itemInfo = WorldDictionary.getItemInfoFromID(registryID);
        if (itemInfo != null && itemInfo.isValid()) {
            String string = itemInfo.getFullType();
            if (string != null) {
                InventoryItem inventoryItem = CreateItem(string, 1.0F, false);
                if (inventoryItem != null) {
                    return inventoryItem;
                }

                DebugLog.log(
                    "InventoryItemFactory.CreateItem() unknown item type \""
                        + (string != null ? string : "unknown")
                        + "\", registry id = \""
                        + registryID
                        + "\". Make sure all mods used in save are installed."
                );
            } else {
                DebugLog.log(
                    "InventoryItemFactory.CreateItem() unknown item with registry ID \"" + registryID + "\". Make sure all mods used in save are installed."
                );
            }
        } else if (itemInfo == null) {
            DebugLog.log(
                "InventoryItemFactory.CreateItem() unknown item with registry ID \"" + registryID + "\". Make sure all mods used in save are installed."
            );
        } else {
            DebugLog.log("InventoryItemFactory.CreateItem() cannot create item: " + itemInfo.ToString());
        }

        return null;
    }
}
